package cuiyt.algorithm.union;

/**
 * @author cyt
 * @create 2020-09-05 16:20
 */
public class UnionQu extends UnionFind {

    public UnionQu(int cap) {
        super(cap);
    }

    @Override
    public void union(int a, int b) {
        int value_A = find(a);
        int value_B = find(b);
        if (value_A == value_B) {
            return;
        }
        parents[value_B] = value_A;
    }

    /**
     * 找到 当前元素的 根
     */
    @Override
    public int find(int a) {
        rangeCheck(a);
//        while (a != parents[a]) {
//            a = parents[a];
//        }
        return parents[a] == a ? a : (parents[a] = find(parents[a]));
    }

}
